package com.cq.hd.common.utils;

import com.cq.hd.common.constant.Constant;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalUnit;
import java.util.Date;

/**
 * @description: 时间工具类
 * @author: mYunYu
 * @date: 2021/7/7 16:04
 * @version: v1.0
 */
@Slf4j
public class LocalDateUtil {

    /**
     * @Description: 时间比较LocalDate
     * @Param: [date1, date2]
     * @Return: java.lang.Integer
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:06
     */
    public static Integer compareLocalDate(LocalDate date1, LocalDate date2) {
        return date1.compareTo(date2);
    }

    /**
     * @Description: 时间比较LocalDateTime
     * @Param: [date1, date2]
     * @Return: java.lang.Integer
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:06
     */
    public static Integer compareLocalDateTime(LocalDateTime date1, LocalDateTime date2) {
        return date1.compareTo(date2);
    }

    /**
     * @Description: 格式化LocalDateTime
     * @Param: [localDateTime, pattern]
     * @Return: java.time.LocalDateTime
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:06
     */
    public static LocalDateTime parseLocalDateTime(LocalDateTime localDateTime, String pattern) {
        return toLocalDateTime(toLocalDateTimeToString(localDateTime, pattern), pattern);
    }

    /**
     * @Description: 格式化LocalDate
     * @Param: [localDate, pattern]
     * @Return: java.time.LocalDate
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:06
     */
    public static LocalDate parseLocalDate(LocalDate localDate, String pattern) {
        return toLocalDate(toLocalDateToString(localDate, pattern), pattern);
    }

    /**
     * @Description: String转localDateTime
     * @Param: [time, pattern]
     * @Return: java.time.LocalDateTime
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static LocalDateTime toLocalDateTime(String time, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(time, dtf);
    }

    /**
     * @Description: String转localDate
     * @Param: [time, pattern]
     * @Return: java.time.LocalDate
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static LocalDate toLocalDate(String time, String pattern) {
        if (time != null) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
            return LocalDate.parse(time, dtf);
        }
        return null;
    }

    /**
     * @Description: LocalDate转String
     * @Param: [time, pattern]
     * @Return: java.lang.String
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static String toLocalDateToString(LocalDate time, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return dtf.format(time);
    }

    /**
     * @Description: LocalDateTime转String
     * @Param: [time, pattern]
     * @Return: java.lang.String
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static String toLocalDateTimeToString(LocalDateTime time, String pattern) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
        return dtf.format(time);
    }

    /**
     * @Description: Long 转 LocalDateTime
     * @Param: [time]
     * @Return: java.time.LocalDateTime
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static LocalDateTime toLongToLocalDateTime(Long time) {
        if (time != null) {
            return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault());
        }
        return null;
    }

    /**
     * @Description: Long 转 LocalDate
     * @Param: [time]
     * @Return: java.time.LocalDate
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static LocalDate toLongToLocalDate(Long time) {
        if (time != null) {
            return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()).toLocalDate();
        }
        return null;
    }

    /**
     * @Description: LocalDate 转 Long
     * @Param: [localDate]
     * @Return: java.lang.Long
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static Long toLocalDateToLong(LocalDate localDate) {
        return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * @Description: LocalDateTime 转 Long
     * @Param: [localDateTime]
     * @Return: java.lang.Long
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static Long toLocalDateTimeToLong(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * @Description: Long类型装String
     * @Param: [time, pattern]
     * @Return: java.lang.String
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static String toTimestampToString(long time, String pattern) {
        return toLocalDateTimeToString(toLongToLocalDateTime(time), pattern);
    }

    /**
     * @Description: 格式化String类型
     * @Param: [date, sourcePattern, targetPattern]
     * @Return: java.lang.String
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:07
     */
    public static String dateFormatForString(String date, String sourcePattern, String targetPattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(sourcePattern);
        formatter.setLenient(false);
        Date newDate = null;
        try {
            newDate = formatter.parse(date);
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        formatter = new SimpleDateFormat(targetPattern);
        return formatter.format(newDate);
    }

    /**
     * @Description: 日期加天数
     * @Param: [time, number, field]
     * @Return: java.time.LocalDate
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:08
     */
    public static LocalDate plus(LocalDate time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }

    /**
     * @Description: 获取时间差 LocalDateTime
     * @Param: [startTime, endTime]
     * @Return: java.time.Duration
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:08
     */
    public static Duration getDurationToLocalDateTime(LocalDateTime startTime, LocalDateTime endTime) {
        return Duration.between(startTime, endTime);
    }

    /**
     * @Description: 获取时间差 LocalDate
     * @Param: [startTime, endTime]
     * @Return: java.time.Duration
     * @Auther: mYunYu
     * @Date: 2021/7/7 16:08
     */
    public static Duration getDurationToLocalDate(LocalDate startTime, LocalDate endTime) {
        return Duration.between(startTime, endTime);
    }

    /**
     * @Description: 获取当前日期时间
     * @Param: []
     * @Return: java.time.LocalDateTime
     * @Auther: mYunYu
     * @Date: 2021/8/16 14:45
     */
    public static LocalDateTime getNowLocalDateTime() {
        return LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
    }

    /**
     * @Description: 将Date 转为 LocalDate
     * @Param: [date]
     * @Return: java.time.LocalDate
     * @Auther: mYunYu
     * @Date: 2021/9/7 15:42
     */
    public static LocalDate dateToLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = instant.atZone(zoneId);

        return zonedDateTime.toLocalDate();
    }

    /**
     * @Description: 将Date 转为 LocalDateTime
     * @Param: [date]
     * @Return: java.time.LocalDateTime
     * @Auther: mYunYu
     * @Date: 2021/9/7 15:42
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = instant.atZone(zoneId);

        return zonedDateTime.toLocalDateTime();
    }

    /**
     * 将yyyy-MM-dd格式日期字符串转为yyyyMMdd格式字符串
     *
     * @param dateStr
     * @return
     */
    public static String stringToString(String dateStr) {
        return LocalDateUtil.toLocalDateToString(LocalDateUtil.toLocalDate(dateStr, Constant.YYYY_MM_DD), "yyyyMMdd");
    }


}
